(function() {
    angular
        .module('ydApp')
        .directive('imagePurview', function($ionicModal, $ionicSlideBoxDelegate , $ionicScrollDelegate, $rootScope) {
            return {
                restrict: "EA",
                scope:{
                    data: '='
                },
                link: function($scope, elem, attr, ngModel) {
                    $scope.$watch('data', function () {
                        $scope.images = $scope.data.images;
                    }, true);
                    $scope.downloadImg = $rootScope.downloadImg;
                    $scope.zoomMin = 1;
                    $scope.activeSlide = 1;
                    $scope.data.showImage = function (index) {
                        $scope.activeSlide = index;
                        $ionicModal.fromTemplateUrl('directive/imagePurview/imagePurview.html', {
                            scope: $scope,
                            animation: 'slide-in-up'
                        }).then(function(modal) {
                            $scope.modal = modal;
                            $scope.modal.show();
                        });
                    }
                    // Close the modal
                    $scope.closeModal = function() {
                        $scope.modal.hide();
                        $scope.modal.remove()
                    };
                    $scope.updateSlideStatus = function(slide) {
                      var zoomFactor = $ionicScrollDelegate.$getByHandle('scrollHandle' + slide).getScrollPosition().zoom;
                      if (zoomFactor == $scope.zoomMin) {
                        $ionicSlideBoxDelegate.enableSlide(true);
                      } else {
                        $ionicSlideBoxDelegate.enableSlide(false);
                      }
                    }; 
                }
            };
        })
})();
